# Code for Table M1, Row 1: Residualized instrument

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('AER')
require('ivpack')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
## Construct residualized instrument
## ---------------------------------------

data0812[, weight := .N, by = c("uniqueid")]
data0812$residuals <- resid(lm(pti ~ as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2), data = data0812, na.action = na.exclude))
data0812[, i_resid := mean(residuals), by = c("uniqueid", "judge_cat", "court_time1", "totOGS2")]
data0812[, i_obs := .N, by = c("uniqueid", "judge_cat", "court_time1", "totOGS2")]
data0812[, tmp_mean := mean(residuals), by = c("judge_cat", "court_time1", "totOGS2")]
data0812[, tmp_obs := .N, by = c("judge_cat", "court_time1", "totOGS2")]
data0812[, judgeiv := (tmp_mean * tmp_obs - (i_obs * i_resid)) / (tmp_obs - i_obs)]
data0812$tmp_mean <- data0812$tmp_obs <- data0812$i_resid <- data0812$i_obs <- NULL

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)


## ---------------------------------------
## Two Stage Least Squares 
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"
 
outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

m1a1 <- ivreg(form.1, data = data0812)
m1a2 <- ivreg(form.2, data = data0812)
m1a3 <- ivreg(form.3, data = data0812)

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a2_d <- summary(m1a2, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)$diagnostics[1, 3]


cont <- data.table(round(rbind(c(robust.se(m1a1)[2, c(1)], robust.se(m1a2)[2, c(1)], robust.se(m1a3)[2, c(1)]),
                               c(robust.se(m1a1)[2, c(2)], robust.se(m1a2)[2, c(2)], robust.se(m1a3)[2, c(2)]),
                               c(m1a1_d, m1a2_d, m1a3_d),
                               c(m1a1$n, m1a2$n, m1a3$n)
),3))

cat("\nPrinting Table M1, row 1: Residualized Instrument...\n")
colnames(cont) <- c("(1)", "(2)", "(3)")
# add row labels
cont$names <-  c("Pretrial Incarceration (PTI)",
                 "Std. Error",
                 "F-test", 
                 "Obs.")
cont <- cont[, c("names", "(1)", "(2)", "(3)")]
print(cont)


